var container = document.getElementById("container");
container.addEventListener("touchmove" , function(event){
	event.preventDefault();
	})


String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");

}

function checkPhone(phone){ 
	if(!(/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone))){ 
	return false; 
	}else{
		return true;
		} 
}




function setCss3 (obj,attrObj) {
   for (var i in attrObj) {
     var newi=i;
	 if(newi.indexOf("-")>0){
	   var num=newi.indexOf("-");
	   newi=newi.replace(newi.substr(num,2),newi.substr(num+1,1).toUpperCase());
	 }
	 obj.style[newi]=attrObj[i];
	 newi=newi.replace(newi.charAt(0),newi.charAt(0).toUpperCase());
	 obj.style["webkit"+newi]=attrObj[i];
	 obj.style["moz"+newi]=attrObj[i];
	 obj.style["o"+newi]=attrObj[i];
	 obj.style["ms"+newi]=attrObj[i];
   }
  }

$(function(){
	//icom.alert("123")
	
	/*$(".content").on("swipeleft" , function(){
		console.log("swipeleft")
		})*/
	
	window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
	
	
	
	var wh = $(window).height();
	var ww = $(window).width();
	console.log(ww);
	var floorArr=[]; // 地板数组
	var lock=false; // 控制播放暂停
	var fw = (ww/wh)*3660+ww-100; // floor 宽度
	var stage=new C3D.Stage();
	stage.size(ww , wh).material({color:""}).update();
	document.getElementById("gamebox").appendChild(stage.el);
	
	//创建一个立体容器
	var space=new C3D.Sprite();
	space.position(0,330,-3660).rotation(-50,0,0).update();
	stage.addChild(space);
	
	for(var i=0;i<6;i++){
		var z = i*300+i*70;
		createFloor({
			z:z
		})
	}
	
	console.log((ww/wh)*3660)
	console.log(ww/wh)
	console.log(1480/2373)
	//插入小牛
	
	/* var animal=new C3D.Plane();
	animal.name('animal').size(250,300).position(0,-1000,1500).rotation(40,0,0).material({color:"",image:"images/animal.png"}).update();
	space.addChild(animal) */
	
	
	function createFloor(options){
		var floor=new C3D.Plane();
		var z = options.z
		
		floor.name('p1'+z).size(fw,300).position(0,0,z).rotation(60,0,0).material({color:"",image:"images/floor.png"}).update();
		space.addChild(floor)
		
		var m=new C3D.Plane();
		var x = Math.random()*450 + 450
		m.name('p1').size(200,300).position(x,-100,150).rotation(0,0,0).material({color:"",image:"images/fc.png"}).update();
		floor.addChild(m)
		
		
		floorArr.push(floor)
		
		
	}
	
	
	
	
	function moveFloor(){
		for(var i=0;i<floorArr.length;i++){
			floorArr[i].z = floorArr[i].z+8
			
			if(floorArr[i].z>=2220){
				floorArr[i].z = 0;	
			}
			floorArr[i].position(0,0,floorArr[i].z).updateT();
		}
	}
	
	
	
	//var animalTop = wh-130;
	//animal.css({top:animalTop})
	
	
	//左右拖动小牛
	var ax = 0
	var amx=0;
	var amy = -1000
	stage.on("touchstart" , function (e){
		//scene.scale(1.3).updateT(); // 不推荐使用 直接通过 js 修改 css 属性 
		//console.log(e.touches[0])
		sx = e.touches[0].pageX
		mx = sx
		ax=animal.x
		console.log(animal.z)
	})
	stage.on("touchmove" , function (e){
		//scene.scale(1).updateT();
		//console.log(e)
		mx = e.touches[0].pageX;
		amx =ax+(mx-sx)*(e.touches[0].radiusX/3.5); 
		if(amx<-(1480/2) || amx>(1480/2)){
			return 
		}
		
		animal.position(amx,amy,1500).updateT();
	})
	
	//*** 小牛 上下跳跃
	var atop = -1000;
	var n=0;
	var astep=-10; //4  118  4
	
	var sx=0;
	var mx=0;
	var aleft=0;
	
	function animalAnim(){
		
		
		if(atop==-1500){
			astep=10
		}else if(atop==-1000){
			astep=-10
		}
		
		atop = atop+astep;
		console.log(atop)
		animal.position(amx,atop,1500).updateT();
		amy = atop;
	}
	
	animate();
	
	function animate(){
		//moveFloor()
		animalAnim()
		requestAnimationFrame(animate);
	}
	
	
	//addEventfn()
	function addEventfn(){
		content.addEventListener("touchstart" , startfn , false)
		content.addEventListener("touchmove" , movefn , false)
		content.addEventListener("touchend" , endfn , false)
	}
	
	function startfn(e){
		aleft = animal[0].offsetLeft;
		sx = e.touches[0].pageX;
		mx = sx;
	}
	function movefn(e){

		mx = e.touches[0].pageX;
		var mleft = aleft+(mx-sx);
		if(mleft<0 || mleft>ww){
			return;
		}
		animal.css({left:mleft})
	}
	function endfn(e){
		
	}
	
	
	
	
	
	})








